package com.yanqu.road.server.cmd.activity.mammonhall;

import com.yanqu.road.entity.activity.mammonhall.MammonHallSourceDataInfo;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.mammonhall.CrossMammonHallMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Cmd(code = Protocol.C_MAMMON_HALL_RANK_TOP, desc = "财神殿-榜首")
public class CrossMammonHallRankTopCmd extends CrossCommand {

    List<Integer> typeList = new ArrayList<>();
    UserBaseInfo userBaseInfo;
    int beginTime;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MammonHallProto.CrossMammonHallRankTopReqMsg reqMsg = MammonHallProto.CrossMammonHallRankTopReqMsg.parseFrom(packet.getBytes());
        typeList = reqMsg.getTypeList();
        userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
        beginTime = reqMsg.getBeginTime();
        return true;
    }


    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        CrossMammonHallMgr.beginTime = beginTime;

        MammonHallProto.MammonHallRankTopRespMsg.Builder respMsg = MammonHallProto.MammonHallRankTopRespMsg.newBuilder();
        for (Map.Entry<Integer, List<MammonHallSourceDataInfo>> listEntry : CrossMammonHallMgr.getSourceDataMap().entrySet()) {
            if(typeList.size() > 0 && !typeList.contains(listEntry.getKey())){
                continue;
            }
            List<MammonHallSourceDataInfo> list = listEntry.getValue();
            if(list.size() == 0){
                continue;
            }
            MammonHallSourceDataInfo dataInfo = list.get(0);
            MammonHallProto.MammonHallRankTemp.Builder topBuilder = CrossMammonHallMgr.getRankTempBuilder(dataInfo);
            UserBaseInfo ubi = CrossUserMgr.getUserBaseInfo(dataInfo.getUserId());
            if(ubi != null){
                topBuilder.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(ubi));
            }
            respMsg.addRank(topBuilder);
        }
        respMsg.setRet(0);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_MAMMON_HALL_RANK_TOP, respMsg));
    }

    @Override
    public void sendData() {

    }


}
